# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from datetime import datetime, timedelta
import time
from openerp.osv import fields, osv
from openerp.tools.translate import _
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT
import openerp.addons.decimal_precision as dp
from openerp import workflow

class sale_order(osv.osv):
    _inherit = "sale.order"
    
    def _get_sum_qty(self, cr, uid, ids, name, arg, context=None):
        if not ids: return {}
        res = {}
        ttl_qty = 0
        for order in self.browse(cr, uid, ids, context=context):
            for line in order.order_line:
                ttl_qty += line.product_uom_qty
                
            res[order.id] = ttl_qty
        return res
    
    _columns = {
        'amount_qty': fields.function(_get_sum_qty, string='Amount Quantity', type='integer'),
        'season':fields.many2one('season', 'Season'),
        'cust_po_no': fields.char('Customer PO#'),
        'order_po_received_date': fields.date('Order/PO received date'),
        'tax_exempt': fields.selection([('y','Yes'),('n','No')],'Tax Exempt'),
        'delivery_date': fields.date('Delivery date'),
        'cancel_date': fields.date('Cancel date'),
        'shipping_method':fields.many2one('shipping.method', 'Shipping Method'),
    }
    
    def print_sales_order(self, cr, uid, ids, context=None):
        if context is None:
           context = {}
        data = self.read(cr, uid, ids)[0]
        self_browse = self.browse(cr, uid, ids)
        
        datas = {
            'ids': [data.get('id')],
            'model': 'sale.order',
            'form': data
            }
        return {
            'type': 'ir.actions.report.xml',
            'report_name': 'belber_module.belber_sale_order',
            'datas': datas,
            'name': self_browse.name
            }
    
class sale_order_line(osv.osv):
    _inherit = "sale.order.line"
    
    def _get_product_desc(self, cr, uid, ids, name, arg, context=None):
        if not ids: return {}
        res = {}
        product_obj = self.pool.get('product.product')
        for order in self.browse(cr, uid, ids, context=context):
            product_id = product_obj.search(cr, uid, [('id','=',order.product_id.id)])
            if product_id:
                desc = model = material = colour = mp = ''
                for detail in product_obj.browse(cr, uid, product_id, context=context):
                    model = detail.belber_product_model_id.description
                    material = detail.belber_product_material_id.description
                    colour = detail.belber_product_colour_id.description
                    mp = detail.belber_metal_code
                    
                desc = model + ' ' + material + ' ' + colour + ' ' + mp
                
            res[order.id] = desc
        return res
    
    _columns = {
        'description': fields.function(_get_product_desc, string='Description', type='char'),
    }

class season(osv.osv):
    _name = 'season'
    _description = 'Season'

    _columns = {
        'name': fields.char('Season', required=True, select=True),
    }

class shipping_method(osv.osv):
    _name = 'shipping.method'
    _description = 'Shipping Method'

    _columns = {
        'name': fields.char('Shipping Method', required=True, select=True),
    }

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
